//
//  CardBlurView.swift
//  miaoyin
//
//  Created by David on 2017/3/11.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class CardBlurView: UIView {

    var cardView: UIView!
    var delegate: CardViewDelegate?
    var closeBtn: UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let bgView = UIVisualEffectView(frame: frame)
        bgView.effect = UIBlurEffect(style: .dark)
        addSubview(bgView)
        
        let blankCloseBtn = UIButton(frame: frame)
        bgView.addSubview(blankCloseBtn)
        blankCloseBtn.addTarget(self, action: #selector(CardBlurView.hideView), for: .touchUpInside)
        
        cardView = UIView(frame: CGRect(x: 32, y: 0, width: frame.width - 64, height: 400))
        cardView.center = CGPoint(x: frame.width/2, y: frame.height/2)
        cardView.backgroundColor = UIColor.white
        cardView.layer.cornerRadius = 8
        bgView.addSubview(cardView)
        
        closeBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        closeBtn.setTitle("×", for: .normal)
        closeBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 24)
        closeBtn.setTitleColor(themeColor2, for: .normal)
        cardView.addSubview(closeBtn)
        closeBtn.addTarget(self, action: #selector(CardBlurView.hideView), for: .touchUpInside)
    }
    
    func hideView(){
        delegate?.cardViewClose?(self)
        UIView.animate(withDuration: 0.3) { 
            self.alpha = 0
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
